package com.fenghongzhang.day002;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

public class Main3Activity extends AppCompatActivity {
    private Button popWin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        popWin = (Button) findViewById(R.id.popWin);

        popWin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupWindow popupWindow = new PopupWindow(Main3Activity.this);

                popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                popupWindow.setHeight(300);

                View inflate = LayoutInflater.from(Main3Activity.this).inflate(R.layout.win_layout, null);
                //xml -> view
                popupWindow.setContentView(inflate);

                //点击外部消失
                popupWindow.setOutsideTouchable(true);
                //绑定动画
                popupWindow.setAnimationStyle(R.style.pop);


                //修改当前窗口的透明度
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 0.5f;
                getWindow().setAttributes(attributes);

                //窗口消失监听器
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        //修改当前窗口的透明度
                        WindowManager.LayoutParams attributes = getWindow().getAttributes();
                        attributes.alpha = 1f;
                        getWindow().setAttributes(attributes);
                    }
                });

                //1,组件  偏移量
//                popupWindow.showAsDropDown(popWin,100,100);

                //1 组件, 2, 组件中的位置,3,4偏移量
                View inflate1 = LayoutInflater.from(Main3Activity.this).inflate(R.layout.activity_main3, null);
                popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);
            }
        });

    }
}
